package com.yunfeng.supercall.httputils;

import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import com.yunfeng.supercall.BuildConfig;

public abstract class MyAjaxCallBack extends AjaxCallBack<String> {

	@Override
	public void onFailure(Throwable t, int errorNo, String strMsg) {
		super.onFailure(t, errorNo, strMsg);
		if (BuildConfig.DEBUG) {
			System.out.println("MyAjaxCallBack.onFailure()" + strMsg);
		}
		onReceiveError(strMsg, errorNo + "");
	}

	@Override
	public void onSuccess(String t) {
		super.onSuccess(t);
		if (BuildConfig.DEBUG) {
			System.out.println("MyAjaxCallBack.onSuccess()" + t);
		}
		JSONObject json;
		try {
			json = new JSONObject(t);
			String msg = "";
			try {
				msg = json.getString("Msg");
			} catch (Exception e) {
				// TODO: handle exception
			}
			String code = json.getString("Code");
			String data = json.getString("Data");
			if (code.equals("0")) {
				onReceiveData(data, msg);
			} else {
				onReceiveError(msg, code);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public abstract void onReceiveData(String data, String msg);

	public abstract void onReceiveError(String msg, String errorCode);
}
